#!/usr/bin/env python
# encoding: utf-8

from app.views.webapi import route_api
from  flask import request,jsonify,g
import requests,json,decimal

from app import db
from app.model import oldComponent
from app.model import oldComponentIn
from app.model import oldComponentOut
from app.model import oldProject

from app.model import oldProduct
from app.model import oldProductBoard
from app.model import oldUserProductBoard


from app.model import User
from app.model import Electronic
from app.model import ElectronicLog
from app.model import ElectronicBoom
from app.model import ElectronicBoomItem

from app.common.libs.Helper import getFormatDate, stringToDatetime, getCurrentDate
from app.common.libs.ElectronicService import ElectronicService
from app.common.libs.UserService import UserService

@route_api.route("/component/move",methods = [ "GET","POST" ])
def componentMove():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    electric = db.session.query(oldComponent, oldProject)\
        .filter(oldComponent.project_id == oldProject.id)\
        .all()
    cnt = 0
    # 复制元器件
    for component,project in electric:
        model          = Electronic()
        model.id       = component.id
        model.name     = component.number
        model.sn       = component.barcode
        model.stock    = component.count
        model.status   = 1
        model.package  = component.package
        model.position = component.position
        model.project  = component.project_id
        model.supplier = component.manufacturer
        model.remark = component.remark
        model.updated_time = model.created_time = getCurrentDate()

        try:
            model.price    = decimal.Decimal(float(component.price))
        except:
            model.price    = decimal.Decimal(0)

        model.project = project.name
        db.session.add(model)
    db.session.commit()
    return jsonify( resp )

@route_api.route("/boom/move",methods = [ "GET","POST" ])
def boomMove():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    res = db.session.query(oldUserProductBoard, oldProduct)\
        .filter(oldUserProductBoard.product_id == oldProduct.id)\
        .all()
    for bom, product in res:
        model = ElectronicBoom()
        model.id = bom.id
        model.name = bom.name
        model.user_id = bom.user_id
        model.note = product.name
        model.updated_time = model.created_time = getCurrentDate()
        db.session.add(model)

    res = db.session.query(oldProductBoard).all()

    for item in res:
        model = ElectronicBoomItem()
        model.id = item.id
        model.count = item.count
        model.electronic_boom_id = item.board_id
        model.electronic_id = item.component_id

        db.session.add(model)

    db.session.commit()
    return jsonify( resp )

@route_api.route("/log/move",methods = [ "GET","POST" ])
def logMove():
    resp = { 'code':20000,'message':'操作成功~', 'data':{}}
    cnt = 0
    res = db.session.query(oldComponentIn).all()
    # 入库记录
    for log in res:
        print(log)
        model = ElectronicLog()
        # model.id = log.id
        model.type = 1
        model.status = 1
        model.electronic_id= log.component_id
        model.operater_id = log.instocker_id
        model.count = log.count
        model.note = log.remark
        model.created_time = log.time
        db.session.add(model)
        cnt += 1
        if cnt>100:
            break

    cnt = 0
    res = db.session.query(oldComponentOut).all()
    for log in res:
        model = ElectronicLog()
        # model.id = log.id
        model.type = 2
        model.status = 1
        model.electronic_id= log.component_id
        model.operater_id = log.outstocker_id
        model.receiver = log.recv
        model.count = log.count
        model.note = log.remark
        model.created_time = log.time
        db.session.add(model)
        cnt += 1
        if cnt>100:
            break

    db.session.commit()
    return jsonify( resp )






